package net.tngou.action.api;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.math.NumberUtils;

import net.tngou.action.BaseAction;
import net.tngou.enums.CacheEnum;
import net.tngou.enums.CountEnum;
import net.tngou.enums.TTypeEnum;
import net.tngou.pojo.User;
import net.tngou.quartz.CacheUtil;
import net.tngou.service.UserService;
import net.tngou.util.DigestMD;
import net.tngou.util.DigestSHA;
import net.tngou.util.OAuthUtil;
import net.tngou.util.StringUtil;
import net.tngou.util.ValidateTools;

public class VoteApi extends BaseAction {

	/**
	 * 投票
	 */
	public void execute() {

		long id = ask.getId();
		String ism = _GetCookieIsm(0);
		if(ism==null)
		{
			run_false("投票失败！");
			return;
		}
		if(session.getAttribute(ism)==null)
		{
			run_false("投票失败！");
			return;
		}
		session.removeAttribute(ism);
		String[] sp = ism.split("-");
		if(sp.length<=3)
		{
			run_false("投票失败！");
			return;
		}
		if(!sp[1].equals(0+""))
		{
			run_false("投票失败！");
			return;
		}
		int length=sp.length;
		if(!NumberUtils.isNumber(sp[length-1]))
		{
			run_false("投票失败！");
			return;
		}
		int vote=Integer.parseInt(sp[length-1])+1;
		if(!sp[length-2].equals(DigestMD.MD2(vote+"")))
		{
			run_false("投票失败！");
			return;
		}
		sp[length-2]=DigestMD.MD2((vote+1)+"");
		if(vote<=10)
		{
			CacheUtil.getInstance(CacheEnum.VisitEh, CountEnum.vcount).Add(id, TTypeEnum.item);
		}
		if(vote>10)vote=10;
		sp[length-1]=vote+"";
		ism="";
		for (String string : sp) {
			ism=ism+string+"-";
		}
		ism=ism.substring(0, ism.length()-1);
		response.addMCookie(0, ism);
		session.setAttribute(ism, "0");
		
		run_success(ism);
	}
	
	
	
	
	
}
